package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Sabrina
 * @Date:2025/06/05/17:15
 * @Description:
 */
@RestController
@RequestMapping("/admin/dish")
@Api("菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;


    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping()
    @ApiOperation("新增菜品")
    public Result insert(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品: {}", dishDTO);//先存菜品再存口味,口味中有dish_id
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(Long[] ids) {
        log.info("批量删除菜品: {}", ids);
        dishService.deleteBatch(ids);//batch批处理
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据ID查询菜品: {}" ,id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        System.out.println(dishVO);
        return Result.success(dishVO);//不应该只查dish一张表,而是应该多表联查,还需要查dishFlavor,而且返回的类型是VO
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品: {}", dishDTO);//先改菜品再改口味(先删再增也是改),口味中有dish_id
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    @PostMapping("status/{status}")
    @ApiOperation("菜品起售停售")
    public Result startAndStop(@PathVariable Integer status, Long id){
        log.info("菜品起售停售: {},{}",status,id);
        dishService.startAndStop(status,id);
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     * @param categotyId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categotyId){
        log.info("根据分类id查询菜品: {}", categotyId);
        List<Dish> list = dishService.list(categotyId);
        return Result.success(list);
    }
}
